package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 上传地理位置
     *
     * @param userId
     * @param longitude
     * @param latitude
     * @param addrStr
     * @return
     */
    @Override
    public Boolean updateLocation(Long userId, Double longitude, Double latitude, String addrStr) {
        try {
            //通过id获取用户地理位置信息
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
            //如果用户位置不存在
            if (location == null) {
                location = new UserLocation();
                location.setUserId(userId);
                location.setAddress(addrStr);
                location.setCreated(System.currentTimeMillis());
                location.setUpdated(System.currentTimeMillis());
                location.setLastUpdated(System.currentTimeMillis());
                location.setLocation(new GeoJsonPoint(longitude, latitude));
                //保存信息
                mongoTemplate.save(location);
            }else {
                //如果用户位置存在，就更新
                Update update = Update.update("location", new GeoJsonPoint(longitude, latitude))
                        .set("updated", System.currentTimeMillis())
                        .set("address",addrStr)
                        .set("lastUpdated", location.getLastUpdated());
                mongoTemplate.updateFirst(query, update, UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 搜附近
     * @param userId
     * @param valueOf
     * @return
     */
    @Override
    public List<Long> search(Long userId, Double metre) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        if (userLocation==null){
            return null;
        }
        //构建圆点
        GeoJsonPoint location = userLocation.getLocation();
        //构建半径
        Distance distance = new Distance(metre/1000, Metrics.KILOMETERS);
        //构建圆
        Circle circle = new Circle(location, distance);
        //扫描这个圆内的用户
        Query query1 = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(query1, UserLocation.class);
        return CollUtil.getFieldValues(userLocations,"userId",Long.class);
    }
}

